﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bettzueche.RLGlue.Codec.Util;
using System.Threading;

namespace RunMinesSarsaExperiment {

    /*
*  A simple example of how can you run all components of the samples-mines-sarsa project from a single Java class.
* This is strictly for the convenience of not having to run three terminal windows and start three 
* separate processes all of the time.  In this case, all three components (agent/environment/experiment)
* are communicating over sockets.
*
* You could use this same approach to run any pair of components too, so if only your agent and environment
* were in Java, or only the agent and experiment, you could save some effort by bundling them like this, 
* and run the final missing component from another language.
*
* See RunMinesSarsaExperimentNoSockets to see how you can use the new setGlue method of the RL-Glue Java Extension
* to run Java Agent/Environment/Experiment without sockets in a single program.
*/
    /// <summary>
    /// Example by Brian Tanner from the Java Glue.
    /// <para>
    ///  A simple example of how can you run all components of the samples-mines-sarsa project from a single Java class.
    ///  This is strictly for the convenience of not having to run three terminal windows and start three 
    ///  separate processes all of the time.  In this case, all three components (agent/environment/experiment)
    ///  are communicating over sockets.
    ///  
    /// You could use this same approach to run any pair of components too, so if only your agent and environment
    /// were in Java, or only the agent and experiment, you could save some effort by bundling them like this, 
    /// and run the final missing component from another language.
    /// 
    /// See RunMinesSarsaExperimentNoSockets to see how you can use the new setGlue method of the RL-Glue Java Extension
    /// to run Java Agent/Environment/Experiment without sockets in a single program.
    /// </para>
    /// </summary>
    class Program {
        static void Main(string[] args) {
            //Create an agentLoader that will start the agent when its run method is called
            AgentLoader theAgentLoader = AgentLoader.LoadAgent(new SampleSarsaAgent());
            //Create an environmentloader that will start the environment when its run method is called
            EnvironmentLoader theEnvironmentLoader = EnvironmentLoader.LoadEnvironment(new SampleMinesEnvironment());
        
            //Create threads so that the agent and environment can run asynchronously 		
            Thread agentThread=new Thread(theAgentLoader.Run);
            Thread environmentThread=new Thread(theEnvironmentLoader.Run);
        
            //Start the threads
            agentThread.Start();
            environmentThread.Start();
        
            //Run the main method of the Sample Experiment, using the arguments were were passed
            //This will run the experiment in the main thread.
            SampleExperiment.Main(args);
            Console.WriteLine("RunMinesSarsaExperiment Complete.");
            Console.WriteLine("Press any key to quit.");
        
            //Quit Program, including stopping the other threads
            Console.ReadKey(false);
            Environment.Exit(1);
        }
    }
}
